import { createRouter, createWebHistory } from 'vue-router';
import LoginView from '@/views/LoginView/LoginView.vue';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Login',
      component: LoginView
    },
    {
      path: '/Register',
      name: 'Register',
      component: () => import('@/views/RegisterView/RegisterView.vue')
    },
    {
      path: '/RegisterSuccess',
      name: 'RegisterSuccess',
      component: () => import('@/views/RegisterView/RegisterSuccess.vue')
    },
    {
      path: '/ChatView',
      name: 'ChatView',
      component: () => import('@/views/ChatView/ChatView.vue')
    },
    {
      path: '/ChatView2',
      name: 'ChatView2',
      component: () => import('@/views/ChatView/ChatView2.vue')
    },
  ]
});
router.beforeEach((to, from, next) => {
  if(to.meta.requireAuth) {
    const token:string = localStorage['token'];
    if(!token) {
      next({path:'/'});
      return;
    }
  }
  next();
});
export default router;
